<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>班级管理</el-breadcrumb-item>
      <el-breadcrumb-item>班级列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索 添加 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入班级名称"
            v-model="queryInfo.query"
            clearable
            @clear="getGradeList"
            @keydown.enter.native="getOneByClassId"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOneByClassId"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addClassDialogVisible = true">添加班级</el-button>
        </el-col>
      </el-row>
      <!-- 班级列表区域 -->
      <el-table :data="gradeList" border stripe>
        <!-- stripe: 斑马条纹
        border：边框-->
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="classid" label="班级编号"></el-table-column>
        <el-table-column prop="major" label="班级名称"></el-table-column>
        <el-table-column prop="college" label="所属院系"></el-table-column>
        <el-table-column prop="grade" label="年级"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              circle
              @click="showEditDialog(scope.row.classid)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              @click="removeById(scope.row.classid)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-size="queryInfo.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination> -->
    </el-card>

    <!-- 添加班级的对话框 -->
    <el-dialog
      title="添加班级"
      :visible.sync="addClassDialogVisible"
      width="30%"
      @close="addDialogClosed"
      @keydown.enter.native="add"
    >
      <!-- 内容主体 -->
      <el-form :model="addClassForm" ref="addClassFormRef" :rules="addClassFormRules" label-width="100px">
        <el-form-item label="班级编码" prop="classid">
          <el-input v-model="addClassForm.classid"></el-input>
        </el-form-item>
        <el-form-item label="班级名称" prop="major">
          <el-input v-model="addClassForm.major"></el-input>
        </el-form-item>

        <el-form-item label="所属院系" prop="college">
          <el-select v-model="addClassForm.college" placeholder="请选择">
            <!-- <el-option
              v-for="item in deptList"
              :key="item.classid"
              :label="item.college"
              :value="item.classid"
            ></el-option> -->
            <el-option label="计算机" value="计算机" deptId="20010"></el-option>
            <el-option label="机械" value="机械" deptId="20011"></el-option>
            <el-option label="服装" value="服装" deptId="20012"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属年级" prop="grade">
          <el-input v-model="addClassForm.grade"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addClassDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改班级的对话框 -->
    <el-dialog
      title="修改班级信息"
      :visible.sync="editClassDialogVisible"
      width="30%"
      @close="editDialogClosed"
      @keydown.enter.native="editUser"
    >
      <!-- 内容主体 -->
      <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="70px">
        <el-form-item label="班级编码">
          <el-input v-model="editForm.classid"></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="editForm.major"></el-input>
        </el-form-item>
        <!-- TODO  -->
        <el-form-item label="所属院系" prop="deptId">
          <el-select v-model="editForm.college" placeholder="请选择">
            <!-- <el-option
              v-for="item in deptList"
              :key="item.id"
              :label="item.college"
              :value="item.id"
            >
            </el-option> -->
            <el-option label="计算机" value="计算机" college="计算机"></el-option>
            <el-option label="机械" value="机械" college="服装"></el-option>
            <el-option label="服装" value="服装" college="机械"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属年级" prop="grade">
          <el-input v-model="addClassForm.grade"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editClassDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 院系列表
      gradeList: [],
      deptList: [],
      // 获取班级列表查询参数对象
      queryInfo: {
        query: ''
        // 当前页数
        //pageNo: 1,
        // 每页显示多少数据
        // pageSize: 5
      },
      // 添加班级对话框
      addClassDialogVisible: false,
      // 班级添加
      addClassForm: {
        classid: '',
        major: '',
        college: '',
        grade: ''
      },
      // 班级添加表单验证规则
      addClassFormRules: {
        classid: [
          { required: true, message: '请输入班级编号', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '班级编号的长度在2～10个字',
            trigger: 'blur'
          }
        ],
        major: [
          { required: true, message: '请输入班级名称', trigger: 'blur' },
          {
            min: 2,
            max: 50,
            message: '班级名称的长度在2～50个字',
            trigger: 'blur'
          }
        ],
        college: [{ required: true, message: '请输入所属院系', trigger: 'blur' }],
        grade: [{ required: true, message: '请输入所属年级', trigger: 'blur' }]
      },
      // 修改班级
      editClassDialogVisible: false,
      editForm: {},
      // 编辑班级表单验证
      editFormRules: {
        classid: [{ required: true, message: '请输入班级编号', trigger: 'blur' }],
        major: [{ required: true, message: '请输入班级名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getGradeList()
    // 加载院系列表
    // this.getDeptList()
  },
  methods: {
    async getGradeList() {
      console.log('getGradeList被调用')
      const { data: res } = await this.$axios.get('/api/admin/getClassInfoAll')
      console.log(res)
      if (!res) {
        return this.$message.error('获取班级列表失败！')
      }
      this.$message.success('获取班级列表成功')
      this.gradeList = res
    },
    async getDeptList() {
      const { data: res } = await this.$http.get('/api/admin/getClassInfoAll')
      if (!res) {
        return this.$message.error('获取院系列表失败！')
      }
      this.deptList = res
    },
    // 监听 添加班级对话框的关闭事件
    addDialogClosed() {
      //this.$refs.addClassFormRef.resetFields()
    },
    // 添加班级
    add() {
      // 提交请求前，表单预验证
      this.$refs.addClassFormRef.validate(async (valid) => {
        // console.log(valid)
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$axios.post('/api/admin/newClass', this.addClassForm)
        if (!res) {
          this.$message.error('添加班级失败！')
          return
        }
        this.$message.success('添加班级成功！')
        // 隐藏添加班级对话框
        this.addClassDialogVisible = false
        this.getGradeList()
      })
    },
    // 编辑班级信息
    async showEditDialog(classid) {
      this.editClassDialogVisible = true
      console.log('classid', classid)
      const { data: res } = await this.$axios.get('/api/admin/getClassInfo', {
        params: {
          classId: classid
        }
      })
      console.log(res)
      // if (!res) {
      //   return this.$message.error('查询班级信息失败！')
      // }
      this.editForm = res
      // this.editClassDialogVisible = true
    },
    // 监听修改班级对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改班级信息
    editUser() {
      // 提交请求前，表单预验证
      this.$refs.editFormRef.validate(async (valid) => {
        // console.log(valid)
        // 表单预校验失败
        console.log('this.=======', this.editForm)
        if (!valid) return console.log(valid)
        const { data: res } = await this.$axios.post('/api/admin/getNewClass', this.editForm)
        console.log('sjdkla', res)
        if (!res) {
          this.$message.error('更新班级信息失败！')
        }
        // 隐藏添加班级对话框
        this.editClassDialogVisible = false
        this.$message.success('更新班级信息成功！')
        this.getGradeList()
      })
    },
    // 删除班级
    async removeById(classid) {
      console.log('classid', classid)
      const confirmResult = await this.$confirm('确定删除该班级?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // 点击确定 返回值为：confirm
      // 点击取消 返回值为： cancel
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$axios.delete('/api/admin/deleteClass', {
        params: {
          classid
        }
      })
      if (res.length === 0) return this.$message.error('删除班级失败！')
      this.$message.success('删除班级成功！')
      this.addClassDialogVisible = false
      // 重新获取用户列表数据
      this.getGradeList()
    },
    // 根据班级id查询班级
    async getOneByClassId() {
      if (this.queryInfo.query) {
        const { data: res } = await this.$axios.get('/api/admin/getClassInfo', {
          params: {
            classId: this.queryInfo.query
          }
        })
        console.log(res)
        this.gradeList = [res]
        this.$message.success('查找成功')
      } else {
        this.$message.error('查找失败')
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
